<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<c:set var="ctx" value="${pageContext.request.contextPath}"/>
<!DOCTYPE html>
<html lang="en">
    <head>        
        <!-- META SECTION -->
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        
        <link rel="icon" href="favicon.ico" type="image/x-icon" />
        <!-- END META SECTION -->
        
        <!-- CSS INCLUDE -->        
        <link rel="stylesheet" href="${ctx}/rs/css/bootstrap-table.css" />
        <link rel="stylesheet" href="${ctx}/rs/css/main.css" />
        <link id="theme" rel="stylesheet" href="${ctx}/rs/joli/css/theme-default.css" />
        <!-- EOF CSS INCLUDE -->       
        <style type="text/css">
            #m-info {
                position: absolute;
                top: 0;left: 0;
                /*background-color: rgba(0,0,0,.7);*/
                width: 350px;height: 400px;
                /*color: #fff;
                padding:10px;
                font-size: 12px;*/
                display: none;
            }
            #m-info img { width:100%;}
        </style>                             
    </head>
<body>
    
    <!-- START PAGE CONTAINER -->
        <div class="page-container">
            <!-- 引入左菜单部分 -->
            <jsp:include page="../joli/menu.jsp"></jsp:include>
            
            <!-- START PAGE CONTENT -->
            <div class="page-content">
                
                <jsp:include page="../joli/top.jsp"></jsp:include>
                
                <!-- START BREADCRUMB -->
                <ul class="breadcrumb">
                    <li><a href="#">媒体推荐</a></li>                    
                    <li class="active">综合广告推荐</li>
                </ul>
                <!-- END BREADCRUMB -->   
                
                
                <!-- PAGE CONTENT WRAPPER -->
                <div class="page-content-wrap">
                    <div class="row">
                        <div class="col-xs-12">
                            <div id="">
                                <div class="fr pb10" >
                                                                                                推荐&nbsp;:
                                    <a href="send.htm?page=compDetail/compDetailStickList">置顶推荐</a>
                                    <a href="send.htm?page=compDetail/CompDetailBannerList"><b>banner推荐</b></a>
                                    <a href="send.htm?page=compDetail/CompDetailTodayList">今日推荐推荐</a>
                                    <input type="hidden" id = "compDetail" value ="2">
                                                      客户名称：<input type="text" class="srt-put1 mr10 widput1" id="user_name" name="user_name"/>
                                      <button class="btn btn-primary btn-sm srt-btn active" onclick="refresh()">搜索</button>
                                </div>
                            </div>
                                <table id="table" cellpadding="0" cellspacing="0" data-toggle="table"
                                    data-url="${ctx}/compDetail/compdetailBannerList.htm"
                                    data-cache="false"
                                    data-pagination="true" 
                                    data-side-pagination="server"
                                    data-page-list="[10, 20, 50, 100]"
                                    data-page-size="20"  
                                    data-query-params="queryParams">
                                    <thead>
                                        <tr class="tab-th">
                                            <!-- <th data-field="state" data-checkbox="true"></th> -->
                                            <th data-field="id" hidden="hidden">编号</th>
                                            <th data-field="user_name" >客户名称</th>
                                            <th data-field="create_time" data-formatter="timeFormatter">提交日期</th>
                                            <th data-field="date" data-formatter="timeFormatter1">推荐日期</th>
                                            <th data-field="sort" data-formatter="sortFormatter">排序</th>
                                            <th data-field="status" data-formatter="statusFormatter">状态</th>
                                            <th data-field="action" data-formatter="actionFormatter" class="col-md-2">操作</th>
                                        </tr>
                                    </thead>
                                </table>
                            </div>
                    </div>
                </div>
        </div>
        <div id="m-info"></div>
    
    
    
    <div class="modal fade" id="modal2" role="dialog" aria-labelledby="gridSystemModalLabel" data-backdrop="static" >
      <div class="modal-dialog">
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            <h4 class="modal-title">提示</h4>
          </div>
          <div class="modal-body">
          <form  class="form-horizontal">
             <div class="form-group">
                <div class="col-sm-10">
                确定推荐此广告？
                </div>
                <div class="col-sm-10">
                     推荐期限<input type="text" class="Wdate search-query w04 startTime" id="start_time" name="repay_date" onFocus="var date=randomDate2();WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss',onpicked:function(){end_time.focus();},minDate:'%y-%M-%d',maxDate:date})" />
            <input type="text" class="Wdate search-query w04 endTime" id="end_time" name="repay_date" onFocus="var date=randomDate();WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss',maxDate:date,minDate:'#F{$dp.$D(\'start_time\')}'})" />
                </div>
              </div>
            </form>  
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
            <button type="button" class="btn btn-success btnAjax">确定</button>
          </div>
        </div>
      </div>
    </div>
    <!-- 引入所有joli的脚本 -->
    <jsp:include page="../joli/scripts.jsp"></jsp:include>
    <!-- 引入页面需要的脚本 -->
    <script type="text/javascript" src="${ctx}/rs/js/bootstrapQ.min.js"></script>
    <script src="${ctx}/rs/js/bootstrap-table.js"></script>
    <script src="${ctx}/rs/js/bootstrap-table-zh-CN.js"></script>
    <script src="${ctx}/rs/js/jquery.formatDate.js"></script>
    <%-- <script src="${ctx}/rs/js/my97DatePicker/WdatePicker.js"></script> --%>
    <script language="javascript" type="text/javascript" src="${ctx}/rs/js/my97DatePicker/WdatePicker.js"></script>
    <script type="text/javascript" src="${ctx}/rs/js/session-out-error.js"></script>
    <script>
      activeMenu($("#menu_media_1"));
          var $table = $('#table');
        
      $table.on('click-row.bs.table', function (e, row, $element) {
            $table.bootstrapTable('uncheckAll');
            $table.bootstrapTable('check', $element.index());
      });
      
      $table.bootstrapTable({
          height: getHeight()
      });
        
      //获取分辨率高度
        function getHeight() {
            return window.screen.height - 308;
        }
        
        $(window).resize(function () {
        $table.bootstrapTable('resetView',{height: getHeight()});
      });
        
        //初始化刷新
         function refresh(pageIndex,selectPage){
             var user_name = $("#user_name").val();
             var compDetail =$("#compDetail").val();
            if(!pageIndex && !selectPage){
                pageIndex = 0;
                selectPage = 0;
            }
            var pageSize = $table.bootstrapTable('getOptions').pageSize;
            pageIndex=pageIndex*pageSize;
            $table.bootstrapTable('refresh', {
                silent: true,
                cache : false, 
                url: '${ctx}/compDetail/compdetailBannerList.htm',
                contentType:'application/x-www-form-urlencoded; charset=UTF-8',
                query: {
                    
                    pageIndex : pageIndex,
                    pageSize : pageSize,
                    user_name : encodeURI(user_name),
                    compDetail:compDetail
                }
           });
            $table.bootstrapTable('selectPage', selectPage);
        }
    
        function reject(id,status){
                  $("#modal1").modal("show");
                  $(".btnAjax").on('click',function(){
                      var reason = $(".reason").val();
                      if(!reason){
                            alert("请填写不通过理由！");
                            return;
                        }if(reason.length>500){
                            alert("理由不能超过500字！");
                            return;
                            
                        }
                      $.ajax({
                           url: "${ctx}/account/reject.htm", 
                          type: 'POST',
                          data: {
                              id:id,
                              status:1,
                              reason:reason
                          },
                            success: function (text) {
                                bootstrapQ.msg({
                                    msg  : '操作成功！',
                                    type : 'success',
                                    time : 2000
                                });
                                setTimeout("window.location.reload(true)",2000);
                            },
                            error: function () {
                                bootstrapQ.msg({
                                    msg  : '操作失败！',
                                    type : 'danger',
                                    time : 2000
                                });
                                setTimeout("window.location.reload(true)",1000);
                            } 
                     }); 
                  })
        }
//         推荐
        function recommend(id,status,put_endtime){
              $("#modal2").modal("show");
              window.maxDate = timeFormatter(put_endtime);
              $(".btnAjax").on('click',function(){
                  var start_time = $("#start_time").val();
                  var end_time = $("#end_time").val();
                  var comp =2;
                  if(start_time==""||end_time==""){
                      alert("请填写推荐时间");
                      return ;
                  }
                  //判断推荐时间是否有冲突
                  $.ajax({
                      url: "${ctx}/compDetail/selectTime.htm", 
                     type: 'POST',
                     data: {
                         comp:comp,
                         end_time:end_time,
                         start_time:start_time
                     },
                     success: function (text) {
                         if(text == "false"){
                             alert("当前推荐时间，已有待推荐，请更换推荐时间！");
                             return ;
                         }else{
			                  $.ajax({
			                       url: "${ctx}/compDetail/recommend.htm", 
			                      type: 'POST',
			                      data: {
			                          comp:comp,
			                          id:id,
			                          status:status,
			                          start_time1:start_time,
			                          end_time1:end_time
			                      },
			                        success: function (text) {
			                        	if(text == "false"){
			                                alert("推荐位已满，暂停原推荐广告后才能继续推荐！");
			                            }else{
			                            bootstrapQ.msg({
			                                msg  : '操作成功！',
			                                type : 'success',
			                                time : 2000
			                            });
			                            }
			                            setTimeout("window.location.reload(true)",2000);
			                        },
			                        error: function () {
			                            bootstrapQ.msg({
			                                msg  : '操作失败！',
			                                type : 'danger',
			                                time : 2000
			                            });
	                                      setTimeout("window.location.reload(true)",1000);
	                                  } 
	                           }); 
	                         }
	                         setTimeout("window.location.reload(true)",2000);
	                     },
	                  });
	              })
	    }
//         /不推荐
            function norecommend(id,status,sort){
                 bootstrapQ.confirm('确定不推荐选中记录？',function(){
                        var comp =2;
                     $.ajax({
                         url: "${ctx}/compDetail/norecommend.htm", 
                         type: 'POST',
                         data: {
                             comp:comp,
                             id:id,
                             status:status,
                             sort:sort
                         },
                           success: function (text) {
                               if(text == "false"){
                            	   bootstrapQ.msg({
                                       msg  : '操作失败！',
                                       type : 'danger',
                                       time : 2000
                                   });
                               }else{
                               bootstrapQ.msg({
                                   msg  : '操作成功！',
                                   type : 'success',
                                   time : 2000
                               });
                               setTimeout("window.location.reload(true)",2000);
                               }
                           },
                           error: function () {
                               bootstrapQ.msg({
                                   msg  : '操作失败！',
                                   type : 'danger',
                                   time : 2000
                               });
                               setTimeout("window.location.reload(true)",1000);
                           } 
                     });
                 });
            }
            //查看详情
            function detail(id) {
                        var compDetail =$("#compDetail").val();
                            bootstrapQ.dialog({
                                url : "${ctx}/compDetail/detail.htm?id="+ id +"&compDetail="+compDetail,
                                title : "广告详情",
                                width: '720px',
                                close : 'true',
                                hidebtn : 'true'
                            },function(){
                                //回调触发关闭窗口
                                $("#bsmodal").modal("hide");
                            });
                    }

        function up(index){
            var pageNumber = $table.bootstrapTable('getOptions').pageNumber;
            var pageIndex = pageNumber - 1;
            var data = $table.bootstrapTable('getData');
            var row = data[index];
            if(index==0){
            	alert("排序已经最靠前");
            	return; 
            }
            var row2 = data[index-1];
            if(row2.status!=1){
                alert("排序已经最靠前");
                return; 
            }
            if(row&&row2){
                $.ajax({
                    url : "${ctx}/compDetail/updateSort.htm?date=" + new Date() + "&sid1=" + row.id+"&value1="+row.sort+"&sid2="+row2.id+"&value2="+row2.sort+"&comp="+2+"&comp_id1="+row.comp_id+"&comp_id2="+row2.comp_id,
                    success: function (text) {
                        refresh(pageIndex,pageNumber);//刷新
                        setTimeout(function () { 
                            $table.bootstrapTable('checkBy', {field:'id', values:[row.id]});
                        }, 200);
                    },
                    error: function () {
                        refresh(pageIndex,pageNumber);
                    }
                });
            }
        }
//        下调
        function down(index){
            var pageNumber = $table.bootstrapTable('getOptions').pageNumber;
            var pageIndex = pageNumber - 1;
            var data = $table.bootstrapTable('getData');
            var row = data[index];
            var row2 = data[index+1];
            if(row2.status!=1){
                alert("推荐排序已经最靠后");
                return ;
            }
            if(row&&row2){
                $.ajax({
                    url : "${ctx}/compDetail/updateSort.htm?date=" + new Date() + "&sid1=" + row.id+"&value1="+row.sort+"&sid2="+row2.id+"&value2="+row2.sort+"&comp="+2+"&comp_id1="+row.comp_id+"&comp_id2="+row2.comp_id,
                    success: function (text) {
                        refresh(pageIndex,pageNumber);//刷新
                        setTimeout(function () { 
                            $table.bootstrapTable('checkBy', {field:'id', values:[row.id]});
                        }, 200);
                    },
                    error: function () {
                        refresh(pageIndex,pageNumber);
                    }
                });
            }
        }
        
        function queryParams(params) {
            return {
                pageIndex : params.offset,
                pageSize : params.limit
            };
        }
//         删除
        function removeData(id) {
            var pageNumber = $table.bootstrapTable('getOptions').pageNumber;
            var pageIndex = pageNumber - 1;
            var rows = $table.bootstrapTable('getSelections');
            var len=$("#table tbody").find("tr").length;
            if(len==rows.length){
                pageIndex = pageIndex - 1;
            }
            if(rows.length > 0){
                var comp =2;
                bootstrapQ.confirm('确定删除选中记录？',function(){
                    $.ajax({
                        url: "${ctx}/compDetail/delete.htm?id="+id+"&comp=" +comp ,
                        success: function (text) {
                            bootstrapQ.msg({
                                msg  : '删除成功！',
                                type : 'success',
                                time : 2000
                            });
                            alert("删除成功");
                            refresh(pageIndex,pageNumber);
                        },
                        error: function () {
                            bootstrapQ.msg({
                                msg  : '删除失败！',
                                type : 'danger',
                                time : 2000
                            });
                            alert("删除失败")
                            refresh(pageIndex,pageNumber);
                        }
                    });
                });
            }
            }
        //提交时间格式化
        function timeFormatter(value, row, index) {
            if (value) {
                return $.formatDate("yyyy-MM-dd HH:mm:ss", new Date(value));
            }
            return "--";
        }
//         期限格式化
        function timeFormatter1(value, row, index){
            var start_time=row.start_time;
            var end_time= row.end_time;
            if(start_time!='' && start_time!=null){
                start_time = $.formatDate("yyyy-MM-dd HH:mm:ss", new Date(start_time));
                end_time = $.formatDate("yyyy-MM-dd HH:mm:ss", new Date(end_time));
                return  "<span class='label label-warning'>"+start_time+"至"+end_time+"</span>"
            }else{
             return "--";
            }
        }
       
//        排序
        function sortFormatter(value, row, index){
            var sort = '';
            if(row.status == 1){
                sort =row.sort;
            }else {
            	sort ="/";
            }
            return sort;
        }
       //状态格式化
        function statusFormatter(value, row, index){
            var statusName ="";
            switch (value){
                case 0:
                    statusName = "<span>未推荐</span>";
                break;
                case 1:
                    statusName = "<span class='label label-primary'>推荐中</span>";
                break;
                case 2:
                    statusName = "<span class='label label-success'>暂停推荐</span>";
                break;
                case 3:
                    statusName = "<span class='label label-warning'>不推荐</span>";
                    break;
                case 4:
                	if(row.start_time==null){
                        statusName = "<span class='label label-danger'>过期</span>";
                        break;
                        }else{
                        statusName = "<span class='label label-danger'>已推荐(过期)</span>";
                        break;
                        }
            }
            return statusName;
        }
        function actionFormatter(value, row, index){
            var retbak="";
            switch(row.status){
            case 0:
                retbak='<a class="mr15" href="javascript:recommend('+row.id+',1,'+row.put_endtime+')">推荐</a><a class="mr15" href="javascript:norecommend('+row.id+',3,'+row.sort+')">不推荐</a><a class="mr15" href="javascript:removeData('+row.id+')">删除</a><a class="mr15" href="javascript:detail('+row.id+')">广告详情</a>';
                break;
            case 1:
                retbak='<a class="mr15" href="javascript:norecommend('+row.id+',2,'+row.sort+')">暂停</a><a class="mr15" href="javascript:up('+index+')">上调</a><a class="mr15" href="javascript:down('+index+')">下调</a><a class="mr15" href="javascript:detail('+row.id+')">广告详情</a>';
                break;
            case 2:
                retbak='<a class="mr15" href="javascript:recommend('+row.id+',1,'+row.put_endtime+')">继续推荐</a><a class="mr15" href="javascript:detail('+row.id+')">广告详情</a>';
                break;
            case 3:
                retbak='<a class="mr15" href="javascript:recommend('+row.id+',1,'+row.put_endtime+')">重新推荐</a><a class="mr15" href="javascript:removeData('+row.id+',5)">删除</a><a class="mr15" href="javascript:detail('+row.id+',5)">广告详情</a>';
                break;
            case 4:
                retbak='<a class="mr15" href="javascript:removeData('+row.id+')">删除</a><a class="mr15" href="javascript:detail('+row.id+',5)">广告详情</a>';
                break;
            }
            return retbak;
        }
        function randomDate(){
            return window.maxDate;
        }
        function randomDate2(){
            return ($("#end_time").val() || window.maxDate);
        }
    </script>
</body>
</html>